package com.learn.annotation_config.autowired.beans.service;

import com.learn.annotation_config.autowired.beans.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("userService")
public class UserServiceImpl implements UserService {
    // @Autowired：当接口存在多个实现类时，以变量名为Bean在容器中的名称去查找对应的Bean注入
    @Autowired
    private UserDao userDao2;
    
    // @Autowired+@Primary：当接口存在多个实现类时，优先注入添加了@Primary注解的Bean
    @Autowired
    private UserDao userDaoPrimary;
    
    // @Autowired+@Qualifier：当接口存在多个实现类时，@Qualifier注解明确指定要注入的Bean
    @Autowired
    @Qualifier("userDao1")
    private UserDao userDaoQualifier;
    
    // @Autowired：用于List上时，表示注入所有接口的实现类
    @Autowired
    private List<UserDao> userDaoList;
    
    // @Autowired：用于方法上时，根据形参而非方法名注入
    @Autowired
    public void setXxx(UserDao userDao2) {
        System.out.println("xxx:" + userDao2); // 2
    }
    
    public void show() {
        System.out.println("userDao2:" + userDao2); // 2
        System.out.println("userDaoPrimary:" + userDaoPrimary); // 2
        System.out.println("userDaoQualifier:" + userDaoQualifier); // 1
        System.out.println("userDaoList:" + userDaoList); // [1, 2]
    }
}
